<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>control_expressions - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Control+Expressions">Control Expressions</a>
    <li><a href="#label-if+Expression"><code>if</code> Expression</a>
    <li><a href="#label-Ternary+if">Ternary if</a>
    <li><a href="#label-unless+Expression"><code>unless</code> Expression</a>
    <li><a href="#label-Modifier+if+and+unless">Modifier <code>if</code> and <code>unless</code></a>
    <li><a href="#label-case+Expression"><code>case</code> Expression</a>
    <li><a href="#label-while+Loop"><code>while</code> Loop</a>
    <li><a href="#label-until+Loop"><code>until</code> Loop</a>
    <li><a href="#label-for+Loop"><code>for</code> Loop</a>
    <li><a href="#label-Modifier+while+and+until">Modifier <code>while</code> and <code>until</code></a>
    <li><a href="#label-break+Statement"><code>break</code> Statement</a>
    <li><a href="#label-next+Statement"><code>next</code> Statement</a>
    <li><a href="#label-redo+Statement"><code>redo</code> Statement</a>
    <li><a href="#label-Modifier+Statements">Modifier Statements</a>
    <li><a href="#label-Flip-Flop">Flip-Flop</a>
    <li><a href="#label-throw-2Fcatch">throw/catch</a>
  </ul>
</div>

  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../NEWS/NEWS-1_8_7.html">NEWS-1.8.7</a>
    <li><a href="../NEWS/NEWS-1_9_1.html">NEWS-1.9.1</a>
    <li><a href="../NEWS/NEWS-1_9_2.html">NEWS-1.9.2</a>
    <li><a href="../NEWS/NEWS-1_9_3.html">NEWS-1.9.3</a>
    <li><a href="../NEWS/NEWS-2_0_0.html">NEWS-2.0.0</a>
    <li><a href="../NEWS/NEWS-2_1_0.html">NEWS-2.1.0</a>
    <li><a href="../NEWS/NEWS-2_2_0.html">NEWS-2.2.0</a>
    <li><a href="../NEWS/NEWS-2_3_0.html">NEWS-2.3.0</a>
    <li><a href="../NEWS/NEWS-2_4_0.html">NEWS-2.4.0</a>
    <li><a href="../NEWS/NEWS-2_5_0.html">NEWS-2.5.0</a>
    <li><a href="../NEWS/NEWS-2_6_0.html">NEWS-2.6.0</a>
    <li><a href="../NEWS/NEWS-2_7_0.html">NEWS-2.7.0</a>
    <li><a href="../NEWS/NEWS-3_0_0_md.html">NEWS-3.0.0</a>
    <li><a href="../NEWS/NEWS-3_1_0_md.html">NEWS-3.1.0</a>
    <li><a href="../NEWS/NEWS-3_2_0_md.html">NEWS-3.2.0</a>
    <li><a href="../bsearch_rdoc.html">bsearch</a>
    <li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
    <li><a href="../case_mapping_rdoc.html">case_mapping</a>
    <li><a href="../character_selectors_rdoc.html">character_selectors</a>
    <li><a href="../command_injection_rdoc.html">command_injection</a>
    <li><a href="../contributing_md.html">contributing</a>
    <li><a href="../contributing/building_ruby_md.html">building_ruby</a>
    <li><a href="../contributing/documentation_guide_md.html">documentation_guide</a>
    <li><a href="../contributing/glossary_md.html">glossary</a>
    <li><a href="../contributing/making_changes_to_ruby_md.html">making_changes_to_ruby</a>
    <li><a href="../contributing/making_changes_to_stdlibs_md.html">making_changes_to_stdlibs</a>
    <li><a href="../contributing/reporting_issues_md.html">reporting_issues</a>
    <li><a href="../contributing/testing_ruby_md.html">testing_ruby</a>
    <li><a href="../dig_methods_rdoc.html">dig_methods</a>
    <li><a href="../distribution_md.html">distribution</a>
    <li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
    <li><a href="../encodings_rdoc.html">encodings</a>
    <li><a href="../extension_ja_rdoc.html">extension.ja</a>
    <li><a href="../extension_rdoc.html">extension</a>
    <li><a href="../fiber_md.html">fiber</a>
    <li><a href="../format_specifications_rdoc.html">format_specifications</a>
    <li><a href="../globals_rdoc.html">globals</a>
    <li><a href="../implicit_conversion_rdoc.html">implicit_conversion</a>
    <li><a href="../keywords_rdoc.html">keywords</a>
    <li><a href="../maintainers_md.html">maintainers</a>
    <li><a href="../marshal_rdoc.html">marshal</a>
    <li><a href="../memory_view_md.html">memory_view</a>
    <li><a href="../optparse/argument_converters_rdoc.html">argument_converters</a>
    <li><a href="../optparse/creates_option_rdoc.html">creates_option</a>
    <li><a href="../optparse/option_params_rdoc.html">option_params</a>
    <li><a href="../optparse/tutorial_rdoc.html">tutorial</a>
    <li><a href="../packed_data_rdoc.html">packed_data</a>
    <li><a href="../ractor_md.html">ractor</a>
    <li><a href="../regexp_rdoc.html">regexp</a>
    <li><a href="../regexp/methods_rdoc.html">methods</a>
    <li><a href="../regexp/unicode_properties_rdoc.html">unicode_properties</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING.html">COPYING</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING_ja.html">COPYING.ja</a>
    <li><a href="../ruby_3_3_0_preview2/LEGAL.html">LEGAL</a>
    <li><a href="../ruby_3_3_0_preview2/NEWS_md.html">NEWS</a>
    <li><a href="../ruby_3_3_0_preview2/README_ja_md.html">README.ja</a>
    <li><a href="../ruby_3_3_0_preview2/README_md.html">README</a>
    <li><a href="../security_rdoc.html">security</a>
    <li><a href="../signals_rdoc.html">signals</a>
    <li><a href="../standard_library_rdoc.html">standard_library</a>
    <li><a href="../strftime_formatting_rdoc.html">strftime_formatting</a>
    <li><a href="../syntax_rdoc.html">syntax</a>
    <li><a href="../syntax/assignment_rdoc.html">assignment</a>
    <li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
    <li><a href="../syntax/comments_rdoc.html">comments</a>
    <li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
    <li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
    <li><a href="../syntax/literals_rdoc.html">literals</a>
    <li><a href="../syntax/methods_rdoc.html">methods</a>
    <li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
    <li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
    <li><a href="../syntax/pattern_matching_rdoc.html">pattern_matching</a>
    <li><a href="../syntax/precedence_rdoc.html">precedence</a>
    <li><a href="../syntax/refinements_rdoc.html">refinements</a>
    <li><a href="../timezones_rdoc.html">timezones</a>
    <li><a href="../windows_md.html">windows</a>
    <li><a href="../yjit/yjit_md.html">yjit</a>
    <li><a href="../yjit/yjit_hacking_md.html">yjit_hacking</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page syntax/control_expressions.rdoc">

<h1 id="label-Control+Expressions">Control Expressions<span><a href="#label-Control+Expressions">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Ruby has a variety of ways to control execution.  All the expressions described here return a value.</p>

<p>For the tests in these control expressions, <code>nil</code> and <code>false</code> are false-values and <code>true</code> and any other object are true-values.  In this document “true” will mean “true-value” and “false” will mean “false-value”.</p>

<h2 id="label-if+Expression"><code>if</code> Expression<span><a href="#label-if+Expression">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The simplest <code>if</code> expression has two parts, a “test” expression and a “then” expression.  If the “test” expression evaluates to a true then the “then” expression is evaluated.</p>

<p>Here is a simple if statement:</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">then</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the test resulted in a true-value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This will print “the test resulted in a true-value”.</p>

<p>The <code>then</code> is optional:</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the test resulted in a true-value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This document will omit the optional <code>then</code> for all expressions as that is the most common usage of <code>if</code>.</p>

<p>You may also add an <code>else</code> expression.  If the test does not evaluate to true the <code>else</code> expression will be executed:</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-keyword">false</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the test resulted in a true-value&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the test resulted in a false-value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This will print “the test resulted in a false-value”.</p>

<p>You may add an arbitrary number of extra tests to an if expression using <code>elsif</code>.  An <code>elsif</code> executes when all tests above the <code>elsif</code> are false.</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>

<span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is zero&quot;</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is one&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is some other value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This will print “a is one” as <code>1</code> is not equal to <code>0</code>. Since <code>else</code> is only executed when there are no matching conditions.</p>

<p>Once a condition matches, either the <code>if</code> condition or any <code>elsif</code> condition, the <code>if</code> expression is complete and no further tests will be performed.</p>

<p>Like an <code>if</code>, an <code>elsif</code> condition may be followed by a <code>then</code>.</p>

<p>In this example only “a is one” is printed:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>

<span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is zero&quot;</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is one&quot;</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">1</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is greater than or equal to one&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is some other value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The tests for <code>if</code> and <code>elsif</code> may have side-effects.  The most common use of side-effect is to cache a value into a local variable:</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> = <span class="ruby-identifier">object</span>.<span class="ruby-identifier">some_value</span>
  <span class="ruby-comment"># do something to a</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The result value of an <code>if</code> expression is the last value executed in the expression.</p>

<h2 id="label-Ternary+if">Ternary if<span><a href="#label-Ternary+if">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You may also write a if-then-else expression using <code>?</code> and <code>:</code>.  This ternary if:</p>

<pre class="ruby"><span class="ruby-identifier">input_type</span> = <span class="ruby-identifier">gets</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/hello/i</span> <span class="ruby-operator">?</span> <span class="ruby-string">&quot;greeting&quot;</span> <span class="ruby-operator">:</span> <span class="ruby-string">&quot;other&quot;</span>
</pre>

<p>Is the same as this <code>if</code> expression:</p>

<pre class="ruby"><span class="ruby-identifier">input_type</span> =
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">gets</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/hello/i</span>
    <span class="ruby-string">&quot;greeting&quot;</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-string">&quot;other&quot;</span>
  <span class="ruby-keyword">end</span>
</pre>

<p>While the ternary if is much shorter to write than the more verbose form, for readability it is recommended that the ternary if is only used for simple conditionals.  Also, avoid using multiple ternary conditions in the same expression as this can be confusing.</p>

<h2 id="label-unless+Expression"><code>unless</code> Expression<span><a href="#label-unless+Expression">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The <code>unless</code> expression is the opposite of the <code>if</code> expression.  If the value is false, the “then” expression is executed:</p>

<pre class="ruby"><span class="ruby-keyword">unless</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the value is a false-value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This prints nothing as true is not a false-value.</p>

<p>You may use an optional <code>then</code> with <code>unless</code> just like <code>if</code>.</p>

<p>Note that the above <code>unless</code> expression is the same as:</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the value is a false-value&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Like an <code>if</code> expression you may use an <code>else</code> condition with <code>unless</code>:</p>

<pre class="ruby"><span class="ruby-keyword">unless</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the value is false&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the value is true&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This prints “the value is true” from the <code>else</code> condition.</p>

<p>You may not use <code>elsif</code> with an <code>unless</code> expression.</p>

<p>The result value of an <code>unless</code> expression is the last value executed in the expression.</p>

<h2 id="label-Modifier+if+and+unless">Modifier <code>if</code> and <code>unless</code><span><a href="#label-Modifier+if+and+unless">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><code>if</code> and <code>unless</code> can also be used to modify an expression.  When used as a modifier the left-hand side is the “then” statement and the right-hand side is the “test” expression:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">zero?</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
</pre>

<p>This will print 1.</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">zero?</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
</pre>

<p>This will print 0.</p>

<p>While the modifier and standard versions have both a “test” expression and a “then” statement, they are not exact transformations of each other due to parse order.  Here is an example that shows the difference:</p>

<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>.<span class="ruby-identifier">zero?</span>
</pre>

<p>This raises the <a href="../NameError.html"><code>NameError</code></a> “undefined local variable or method ‘a’”.</p>

<p>When ruby parses this expression it first encounters <code>a</code> as a method call in the “then” expression, then later it sees the assignment to <code>a</code> in the “test” expression and marks <code>a</code> as a local variable.</p>

<p>When running this line it first executes the “test” expression, <code>a = 0.zero?</code>.</p>

<p>Since the test is true it executes the “then” expression, <code>p a</code>. Since the <code>a</code> in the body was recorded as a method which does not exist the <a href="../NameError.html"><code>NameError</code></a> is raised.</p>

<p>The same is true for <code>unless</code>.</p>

<h2 id="label-case+Expression"><code>case</code> Expression<span><a href="#label-case+Expression">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The <code>case</code> expression can be used in two ways.</p>

<p>The most common way is to compare an object against multiple patterns.  The patterns are matched using the +===+ method which is aliased to +==+ on <a href="../Object.html"><code>Object</code></a>.  Other classes must override it to give meaningful behavior.  See <a href="../Module.html#method-i-3D-3D-3D"><code>Module#===</code></a> and <a href="../Regexp.html#method-i-3D-3D-3D"><code>Regexp#===</code></a> for examples.</p>

<p>Here is an example of using <code>case</code> to compare a <a href="../String.html"><code>String</code></a> against a pattern:</p>

<pre class="ruby"><span class="ruby-keyword">case</span> <span class="ruby-string">&quot;12345&quot;</span>
<span class="ruby-keyword">when</span> <span class="ruby-regexp">/^1/</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the string starts with one&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;I don&#39;t know what the string starts with&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Here the string <code>&quot;12345&quot;</code> is compared with <code>/^1/</code> by calling <code>/^1/ === &quot;12345&quot;</code> which returns <code>true</code>.  Like the <code>if</code> expression, the first <code>when</code> that matches is executed and all other matches are ignored.</p>

<p>If no matches are found, the <code>else</code> is executed.</p>

<p>The <code>else</code> and <code>then</code> are optional, this <code>case</code> expression gives the same result as the one above:</p>

<pre class="ruby"><span class="ruby-keyword">case</span> <span class="ruby-string">&quot;12345&quot;</span>
<span class="ruby-keyword">when</span> <span class="ruby-regexp">/^1/</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the string starts with one&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You may place multiple conditions on the same <code>when</code>:</p>

<pre class="ruby"><span class="ruby-keyword">case</span> <span class="ruby-string">&quot;2&quot;</span>
<span class="ruby-keyword">when</span> <span class="ruby-regexp">/^1/</span>, <span class="ruby-string">&quot;2&quot;</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;the string starts with one or is &#39;2&#39;&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Ruby will try each condition in turn, so first <code>/^1/ === &quot;2&quot;</code> returns <code>false</code>, then <code>&quot;2&quot; === &quot;2&quot;</code> returns <code>true</code>, so “the string starts with one or is ‘2’” is printed.</p>

<p>You may use <code>then</code> after the <code>when</code> condition.  This is most frequently used to place the body of the <code>when</code> on a single line.</p>

<pre class="ruby"><span class="ruby-keyword">case</span> <span class="ruby-identifier">a</span>
<span class="ruby-keyword">when</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span> <span class="ruby-keyword">then</span> <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is one or two&quot;</span>
<span class="ruby-keyword">when</span> <span class="ruby-value">3</span>    <span class="ruby-keyword">then</span> <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is three&quot;</span>
<span class="ruby-keyword">else</span>           <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;I don&#39;t know what a is&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The other way to use a <code>case</code> expression is like an if-elsif expression:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">2</span>

<span class="ruby-keyword">case</span>
<span class="ruby-keyword">when</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is one or two&quot;</span>
<span class="ruby-keyword">when</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-value">3</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;a is three&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;I don&#39;t know what a is&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Again, the <code>then</code> and <code>else</code> are optional.</p>

<p>The result value of a <code>case</code> expression is the last value executed in the expression.</p>

<p>Since Ruby 2.7, <code>case</code> expressions also provide a more powerful experimental pattern matching feature via the <code>in</code> keyword:</p>

<pre class="ruby"><span class="ruby-keyword">case</span> {<span class="ruby-value">a:</span> <span class="ruby-value">1</span>, <span class="ruby-value">b:</span> <span class="ruby-value">2</span>, <span class="ruby-value">c:</span> <span class="ruby-value">3</span>}
<span class="ruby-keyword">in</span> <span class="ruby-value">a:</span> <span class="ruby-constant">Integer</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">m</span>
  <span class="ruby-node">&quot;matched: #{m}&quot;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-string">&quot;not matched&quot;</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; &quot;matched: 1&quot;</span>
</pre>

<p>The pattern matching syntax is described on <a href="pattern_matching_rdoc.html">its own page</a>.</p>

<h2 id="label-while+Loop"><code>while</code> Loop<span><a href="#label-while+Loop">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The <code>while</code> loop executes while a condition is true:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-keyword">while</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
</pre>

<p>Prints the numbers 0 through 10.  The condition <code>a &lt; 10</code> is checked before the loop is entered, then the body executes, then the condition is checked again.  When the condition results in false the loop is terminated.</p>

<p>The <code>do</code> keyword is optional.  The following loop is equivalent to the loop above:</p>

<pre class="ruby"><span class="ruby-keyword">while</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The result of a <code>while</code> loop is <code>nil</code> unless <code>break</code> is used to supply a value.</p>

<h2 id="label-until+Loop"><code>until</code> Loop<span><a href="#label-until+Loop">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The <code>until</code> loop executes while a condition is false:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-keyword">until</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">10</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
</pre>

<p>This prints the numbers 0 through 11.  Like a while loop the condition <code>a &gt; 10</code> is checked when entering the loop and each time the loop body executes.  If the condition is false the loop will continue to execute.</p>

<p>Like a <code>while</code> loop, the <code>do</code> is optional.</p>

<p>Like a <code>while</code> loop, the result of an <code>until</code> loop is nil unless <code>break</code> is used.</p>

<h2 id="label-for+Loop"><code>for</code> Loop<span><a href="#label-for+Loop">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The <code>for</code> loop consists of <code>for</code> followed by a variable to contain the iteration argument followed by <code>in</code> and the value to iterate over using each. The <code>do</code> is optional:</p>

<pre class="ruby"><span class="ruby-keyword">for</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">in</span> [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>] <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-identifier">value</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Prints 1, 2 and 3.</p>

<p>Like <code>while</code> and <code>until</code>, the <code>do</code> is optional.</p>

<p>The <code>for</code> loop is similar to using each, but does not create a new variable scope.</p>

<p>The result value of a <code>for</code> loop is the value iterated over unless <code>break</code> is used.</p>

<p>The <code>for</code> loop is rarely used in modern ruby programs.</p>

<h2 id="label-Modifier+while+and+until">Modifier <code>while</code> and <code>until</code><span><a href="#label-Modifier+while+and+until">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Like <code>if</code> and <code>unless</code>, <code>while</code> and <code>until</code> can be used as modifiers:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span> <span class="ruby-comment"># prints 10</span>
</pre>

<p><code>until</code> used as a modifier:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">until</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">10</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span> <span class="ruby-comment"># prints 11</span>
</pre>

<p>You can use <code>begin</code> and <code>end</code> to create a <code>while</code> loop that runs the body once before the condition:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-keyword">begin</span>
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span> <span class="ruby-comment"># prints 10</span>
</pre>

<p>If you don’t use <code>rescue</code> or <code>ensure</code>, Ruby optimizes away any exception handling overhead.</p>

<h2 id="label-break+Statement"><code>break</code> Statement<span><a href="#label-break+Statement">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Use <code>break</code> to leave a block early.  This will stop iterating over the items in <code>values</code> if one of them is even:</p>

<pre class="ruby"><span class="ruby-identifier">values</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">even?</span>

  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You can also terminate from a <code>while</code> loop using <code>break</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">0</span>

<span class="ruby-keyword">while</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>

  <span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>
</pre>

<p>This prints the numbers 0 and 1.</p>

<p><code>break</code> accepts a value that supplies the result of the expression it is “breaking” out of:</p>

<pre class="ruby"><span class="ruby-identifier">result</span> = [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">break</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">even?</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">result</span> <span class="ruby-comment"># prints 4</span>
</pre>

<h2 id="label-next+Statement"><code>next</code> Statement<span><a href="#label-next+Statement">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Use <code>next</code> to skip the rest of the current iteration:</p>

<pre class="ruby"><span class="ruby-identifier">result</span> = [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">even?</span>

  <span class="ruby-identifier">value</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">result</span> <span class="ruby-comment"># prints [2, nil, 6]</span>
</pre>

<p><code>next</code> accepts an argument that can be used as the result of the current block iteration:</p>

<pre class="ruby"><span class="ruby-identifier">result</span> = [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">next</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">even?</span>

  <span class="ruby-identifier">value</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">result</span> <span class="ruby-comment"># prints [2, 2, 6]</span>
</pre>

<h2 id="label-redo+Statement"><code>redo</code> Statement<span><a href="#label-redo+Statement">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Use <code>redo</code> to redo the current iteration:</p>

<pre class="ruby"><span class="ruby-identifier">result</span> = []

<span class="ruby-keyword">while</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">10</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span>

  <span class="ruby-keyword">redo</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">even?</span>

  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">result</span>
</pre>

<p>This prints [0, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11]</p>

<p>In Ruby 1.8, you could also use <code>retry</code> where you used <code>redo</code>.  This is no longer true, now you will receive a <a href="../SyntaxError.html"><code>SyntaxError</code></a> when you use <code>retry</code> outside of a <code>rescue</code> block.  See <a href="exceptions_rdoc.html">Exceptions</a> for proper usage of <code>retry</code>.</p>

<h2 id="label-Modifier+Statements">Modifier Statements<span><a href="#label-Modifier+Statements">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Ruby’s grammar differentiates between statements and expressions.  All expressions are statements (an expression is a type of statement), but not all statements are expressions.  Some parts of the grammar accept expressions and not other types of statements, which causes code that looks similar to be parsed differently.</p>

<p>For example, when not used as a modifier, <code>if</code>, <code>else</code>, <code>while</code>, <code>until</code>, and <code>begin</code> are expressions (and also statements).  However, when used as a modifier, <code>if</code>, <code>else</code>, <code>while</code>, <code>until</code> and <code>rescue</code> are statements but not expressions.</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>; <span class="ruby-value">1</span> <span class="ruby-keyword">end</span> <span class="ruby-comment"># expression (and therefore statement)</span>
<span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>      <span class="ruby-comment"># statement (not expression)</span>
</pre>

<p>Statements that are not expressions cannot be used in contexts where an expression is expected, such as method arguments.</p>

<pre>puts( 1 if true )      #=&gt; SyntaxError</pre>

<p>You can wrap a statement in parentheses to create an expression.</p>

<pre class="ruby"><span class="ruby-identifier">puts</span>((<span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>))      <span class="ruby-comment">#=&gt; 1</span>
</pre>

<p>If you put a space between the method name and opening parenthesis, you do not need two sets of parentheses.</p>

<pre class="ruby"><span class="ruby-identifier">puts</span> (<span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>)       <span class="ruby-comment">#=&gt; 1, because of optional parentheses for method</span>
</pre>

<p>This is because this is parsed similar to a method call without parentheses.  It is equivalent to the following code, without the creation of a local variable:</p>

<pre class="ruby"><span class="ruby-identifier">x</span> = (<span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">x</span>
</pre>

<p>In a modifier statement, the left-hand side must be a statement and the right-hand side must be an expression.</p>

<p>So in <code>a if b rescue c</code>, because <code>b rescue c</code> is a statement that is not an expression, and therefore is not allowed as the right-hand side of the <code>if</code> modifier statement, the code is necessarily parsed as <code>(a if b) rescue c</code>.</p>

<p>This interacts with operator precedence in such a way that:</p>

<pre class="ruby"><span class="ruby-identifier">stmt</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">v</span> = <span class="ruby-identifier">expr</span> <span class="ruby-keyword">rescue</span> <span class="ruby-identifier">x</span>
<span class="ruby-identifier">stmt</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">v</span> = <span class="ruby-identifier">expr</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">x</span>
</pre>

<p>are parsed as:</p>

<pre class="ruby"><span class="ruby-identifier">stmt</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">v</span> = (<span class="ruby-identifier">expr</span> <span class="ruby-keyword">rescue</span> <span class="ruby-identifier">x</span>)
(<span class="ruby-identifier">stmt</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">v</span> = <span class="ruby-identifier">expr</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">x</span>
</pre>

<p>This is because modifier <code>rescue</code> has higher precedence than <code>=</code>, and modifier <code>if</code> has lower precedence than <code>=</code>.</p>

<h2 id="label-Flip-Flop">Flip-Flop<span><a href="#label-Flip-Flop">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The flip-flop is a slightly special conditional expression.  One of its typical uses is processing text from ruby one-line programs used with <code>ruby -n</code> or <code>ruby -p</code>.</p>

<p>The form of the flip-flop is an expression that indicates when the flip-flop turns on, <code>..</code> (or <code>...</code>), then an expression that indicates when the flip-flop will turn off.  While the flip-flop is on it will continue to evaluate to <code>true</code>, and <code>false</code> when off.</p>

<p>Here is an example:</p>

<pre class="ruby"><span class="ruby-identifier">selected</span> = []

<span class="ruby-value">0</span>.<span class="ruby-identifier">upto</span> <span class="ruby-value">10</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">selected</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">2</span><span class="ruby-operator">..</span><span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">8</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">selected</span> <span class="ruby-comment"># prints [2, 3, 4, 5, 6, 7, 8]</span>
</pre>

<p>In the above example, the ‘on’ condition is <code>n==2</code>.  The flip-flop is initially ‘off’ (false) for 0 and 1, but becomes ‘on’ (true) for 2 and remains ‘on’ through 8.  After 8 it turns off and remains ‘off’ for 9 and 10.</p>

<p>The flip-flop must be used inside a conditional such as <code>!</code>, <code>? :</code>, <code>not</code>, <code>if</code>, <code>while</code>, <code>unless</code>, <code>until</code> etc. including the modifier forms.</p>

<p>When you use an inclusive range (<code>..</code>), the ‘off’ condition is evaluated when the ‘on’ condition changes:</p>

<pre class="ruby"><span class="ruby-identifier">selected</span> = []

<span class="ruby-value">0</span>.<span class="ruby-identifier">upto</span> <span class="ruby-value">5</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">selected</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">2</span><span class="ruby-operator">..</span><span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">selected</span> <span class="ruby-comment"># prints [2]</span>
</pre>

<p>Here, both sides of the flip-flop are evaluated so the flip-flop turns on and off only when <code>value</code> equals 2.  Since the flip-flop turned on in the iteration it returns true.</p>

<p>When you use an exclusive range (<code>...</code>), the ‘off’ condition is evaluated on the following iteration:</p>

<pre class="ruby"><span class="ruby-identifier">selected</span> = []

<span class="ruby-value">0</span>.<span class="ruby-identifier">upto</span> <span class="ruby-value">5</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">selected</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">2</span><span class="ruby-operator">...</span><span class="ruby-identifier">value</span><span class="ruby-operator">==</span><span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">selected</span> <span class="ruby-comment"># prints [2, 3, 4, 5]</span>
</pre>

<p>Here, the flip-flop turns on when <code>value</code> equals 2, but doesn’t turn off on the same iteration.  The ‘off’ condition isn’t evaluated until the following iteration and <code>value</code> will never be two again.</p>

<h2 id="label-throw-2Fcatch">throw/catch<span><a href="#label-throw-2Fcatch">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><code>throw</code> and <code>catch</code> are used to implement non-local control flow in Ruby. They operate similarly to exceptions, allowing control to pass directly from the place where <code>throw</code> is called to the place where the matching <code>catch</code> is called. The main difference between <code>throw</code>/<code>catch</code> and the use of exceptions is that <code>throw</code>/<code>catch</code> are designed for expected non-local control flow, while exceptions are designed for exceptional control flow situations, such as handling unexpected errors.</p>

<p>When using <code>throw</code>, you provide 1-2 arguments.  The first argument is the value for the matching <code>catch</code>.  The second argument is optional (defaults to <code>nil</code>), and will be the value that <code>catch</code> returns if there is a matching <code>throw</code> inside the <code>catch</code> block.  If no matching <code>throw</code> method is called inside a <code>catch</code> block, the <code>catch</code> method returns the return value of the block passed to it.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">a</span>(<span class="ruby-identifier">n</span>)
  <span class="ruby-identifier">throw</span> <span class="ruby-value">:d</span>, <span class="ruby-value">:a</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">n</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
  <span class="ruby-identifier">b</span>(<span class="ruby-identifier">n</span>)
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">b</span>(<span class="ruby-identifier">n</span>)
  <span class="ruby-identifier">throw</span> <span class="ruby-value">:d</span>, <span class="ruby-value">:b</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">n</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
  <span class="ruby-identifier">c</span>(<span class="ruby-identifier">n</span>)
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">c</span>(<span class="ruby-identifier">n</span>)
  <span class="ruby-identifier">throw</span> <span class="ruby-value">:d</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">n</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>

<span class="ruby-value">4</span>.<span class="ruby-identifier">times</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">catch</span>(<span class="ruby-value">:d</span>) <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">a</span>(<span class="ruby-identifier">i</span>)
    <span class="ruby-value">:default</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; [:a, :b, nil, :default]</span>
</pre>

<p>If the first argument you pass to <code>throw</code> is not handled by a matching <code>catch</code>, an <a href="../UncaughtThrowError.html"><code>UncaughtThrowError</code></a> exception will be raised.  This is because <code>throw</code>/<code>catch</code> should only be used for expected control flow changes, so using a value that is not already expected is an error.</p>

<p><code>throw</code>/<code>catch</code> are implemented as <a href="../Kernel.html"><code>Kernel</code></a> methods (<a href="../Kernel.html#method-i-throw"><code>Kernel#throw</code></a> and <a href="../Kernel.html#method-i-catch"><code>Kernel#catch</code></a>), not as keywords. So they are not usable directly if you are in a <a href="../BasicObject.html"><code>BasicObject</code></a> context.  You can use <a href="../Kernel.html#method-i-throw"><code>Kernel.throw</code></a> and <a href="../Kernel.html#method-i-catch"><code>Kernel.catch</code></a> in this case:</p>

<pre class="ruby"><span class="ruby-constant">BasicObject</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">instance_exec</span> <span class="ruby-keyword">do</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">a</span>
    <span class="ruby-identifier">b</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">b</span>
    <span class="ruby-identifier">c</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">c</span>
    <span class="ruby-operator">::</span><span class="ruby-constant">Kernel</span>.<span class="ruby-identifier">throw</span> <span class="ruby-value">:d</span>, <span class="ruby-value">:e</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">result</span> = <span class="ruby-operator">::</span><span class="ruby-constant">Kernel</span>.<span class="ruby-identifier">catch</span>(<span class="ruby-value">:d</span>) <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">a</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span> <span class="ruby-comment"># =&gt; :e</span>
<span class="ruby-keyword">end</span>
</pre>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

